import {
	resetAllStores,
	useAccountStore,
	useAppStore,
	useCurrencyStore,
	useLayoutStore
} from '@/store'
import { initCS, updateCS } from './cs'
import { getItem, getQuery, updateCSLocation } from '@/utils'
import router from '@/router'
import { ThirdCallbackParams } from '@/api/account-type'

export const initApp = async () => {
	const appStore = useAppStore()
	const accountStore = useAccountStore()
	const currencyStore = useCurrencyStore()
	const layoutStore = useLayoutStore()
	// const memberStore = useMemberStore()
	// const bettingConfigStore = useBettingConfigStore()
	// const activityStore = useActivityStore()
	// const sportsStore = useSportsStore()
	// const marketStore = useMarketStore()
	initAgent().then()
	initThirdRegister().then()
	initCS()
	appStore.showLock(!getItem('UNLOCK'))
	if (!appStore.uuid) appStore.setUUID()
	// 持久化导致存储的游戏中状态关闭页面，重新开启一个新的页面状态还是游戏中的bug
	appStore.setGaming(false)
	appStore.getMobileAreaOptions()
	//appStore.getIpInfo()
	currencyStore.getExchangeRate().then()
	currencyStore.getCurrencyList().then()
	if (accountStore.token) accountStore.getAccountInfo().then()
	if (!accountStore.isLogin) resetAllStores()
	//activityStore.getStarList()
	//activityStore.getVipList()
	//sportsStore.getSports()
	//sportsStore.getHotLeagues()
	//sportsStore.getRecommendLeagues()
	//marketStore.getMarkets()
	//bettingConfigStore.getConfigs()
	//bettingConfigStore.getQuickInputList()
	updateCSLocation(!!layoutStore.sidebar)
}

// export const socketReconnected = async () => {
// 	// const appStore = useAppStore()
// 	const accountStore = useAccountStore()
// 	const profileStore = useProfileStore()
// 	const walletStore = useWalletStore()
// 	// const matchStore = useMatchStore()
// 	if (accountStore.token) {
// 		auth({ authorization: `Bearer ${accountStore.token}` })
// 		// setOdds(appStore.odds)
// 		profileStore.getUserInfo().then()
// 		walletStore.getBalance().then()
// 		// appStore.getNoticeUnread()
// 		// matchStore.updateMatches()
// 	}
// }

const initAgent = async () => {
	// console.log(location.pathname === '/register')
	const appStore = useAppStore()
	const accountStore = useAccountStore()
	const from = getQuery('from')
	if (!from) {
		return
	}
	const inviteId = getQuery('inviteId') || ''
	appStore.setAgent({
		from,
		inviteId
	})
	if (accountStore.isLogin) {
		await accountStore.logout()
	}
	setTimeout(() => {
		accountStore.showRegister()
	})
}

const initThirdRegister = async () => {
	const accountStore = useAccountStore()
	if (location.href.includes('/social-callback')) {
		accountStore.thirdCallbackLink = location.href
		const { code, source, state } = router.resolve(location.href).query
		accountStore.thirdCallbackParams = <ThirdCallbackParams>{
			redirectUri: `${location.origin}/social-callback?source=${source}`,
			socialCode: code,
			socialState: state,
			source
		}
		if (accountStore.isLogin) {
			await accountStore.logout()
		}
		// 弹窗的开启 放到 redirect.vue 里面了; 更好兼容多H5和PC的处理
		// setTimeout(() => {
		// 	accountStore.showThirdRegister()
		// })
	}
}

export const customerService = async () => {
	updateCS(false)
}

export async function paste() {
	try {
		return await navigator.clipboard.readText()
	} catch (err) {
		console.log('粘贴失败！', err)
	}
}

export function share(
	data = {
		title: '',
		text: '',
		url: '/'
	}
) {
	if (!navigator.share) {
		// showToast('app.browserNotSupport')
		return
	}
	return navigator.share(data)
}

export function todo() {
	const appStore = useAppStore()
	appStore.showTodo(true)
}

export const sleep = (delay: number) => new Promise((resolve) => setTimeout(resolve, delay))
